Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve setItem performance on iOS #16

Merged
merged 1 commit into from
Sep 24, 2024

Conversation

IvanIhnatsiuk
Copy link
Owner

📜 Description

In this update, I replaced the original approach of attempting an item add followed by a conditional update in case of failure with a simplified approach that deletes the existing item and then adds the new one. This refactor applies to secure storage management, specifically replacing the second code snippet (conditional logic) with the first code snippet (delete-then-add).

Before: The code attempted to add an item using SecItemAdd(). If it failed due to the item already existing (e.g., status != errSecSuccess), it fell back to SecItemUpdate() to modify the existing item. This involved creating an additional updateDictionary and invoking SecItemUpdate(), increasing complexity and overhead.
After: The code now first deletes any existing item using SecItemDelete() and then unconditionally adds the new item using SecItemAdd(). This avoids the need for conditional checks and extra logic for updates.

Rationale for the Change:
Performance Optimization:

The previous code path had to handle two cases: adding a new item and updating an existing item. This resulted in multiple system calls (one for SecItemAdd() and one for SecItemUpdate() in case of failure), adding both complexity and time overhead.
By simplifying the flow to always delete the existing item first, we eliminate the need for conditionally updating the item. This reduces the number of operations and the need for additional dictionary manipulations.
Expected Performance Improvement: Fewer conditional checks and system calls should lead to faster execution, especially when items frequently exist and require updates.
Simplified Logic:

The new approach is more straightforward: instead of handling two separate cases (add vs. update), the logic always deletes and then adds the item. This reduces the cognitive load for anyone maintaining the code in the future, as the flow is linear and predictable.
Reduced Complexity:

In the original approach, failure of SecItemAdd() led to a fallback that required creating an updateDictionary and making a separate SecItemUpdate() call. This additional branching and dictionary creation added complexity and increased the risk of mistakes or maintenance issues.
The new approach avoids extra dictionary allocations and keeps the code simpler and more maintainable.

💡 Motivation and Context

Performance comprassion

set 1000 items to secure storage

set 1000 items to secure storage create + update delete + create
average time: 2.259 1.892
median time: 2 2

🤔 How Has This Been Tested?

iPhone 15 Pro

📝 Checklist

  • CI successfully passed

@IvanIhnatsiuk IvanIhnatsiuk self-assigned this Sep 24, 2024
@IvanIhnatsiuk IvanIhnatsiuk force-pushed the feat/improve-set-item-performance branch from bdaabb2 to cc2e003 Compare September 24, 2024 14:16
@IvanIhnatsiuk IvanIhnatsiuk merged commit 9171077 into main Sep 24, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant